package com.whf.android.jar.web.utils;

import android.os.Build;
import android.text.Html;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 转义字符 &lt； &gt； 等等
 *
 * @author qf
 * @version 1.0.5
 */
public final class HtmlUtils {

    /**
     * 转义字符 &lt； &gt； 等等
     */
    @NonNull
    public static String toHtml(String content) {
        if (ObjectUtils.isEmpty(content)) {
            return "";
        }
        if (content.contains("text-indent:2em;") || content.contains("text-indent: 2em;") ||
                content.contains("<figure class=\"image\">")) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                content = Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY).toString();
            }
        }
        return content.replaceAll("&nbsp;", " ")
                .replaceAll("&lt;", "<")
                .replaceAll("&gt;", ">")
                .replaceAll("&quot;", "\"")
                .replaceAll("&apos;", "'")
                .replaceAll("&#39;", "'")
                .replaceAll("&amp;", "&")
                .replaceAll("&cent;", "分")
                .replaceAll("&pound;", "镑");
    }

    /**
     * @param filePath:是否是pdf
     */
    public static boolean isPdfJs(String filePath) {
        if (ObjectUtils.isEmpty(filePath)) {
            return false;
        } else {
            String type = filePath.substring(filePath.lastIndexOf(".") + 1);
            String[] strings = new String[]{"pdf", "doc", "docx", "xls", "xlsx", "txt"};
            List<String> test = Arrays.asList(strings);
            return test.contains(type);
        }
    }
}
